var app = angular.module("app", ["ngSanitize"]);
app.controller("controller", function ($scope) {
  $scope.data = {
    message: {},
    home: {},
    fire: {},
    security: {
      monitor: [{}, {}, {}, {}],
    },
    equipment: {},
    operate: {},
    maintain: {},
    save: {},
  };
  $scope.deviceData = {};
  $scope.device = {};
  $scope.glassDelay = 600;
  $scope.time = "00:00:00";
  $scope.messageClass = "blink";
  $scope.messageType = "alarm";
  $scope.bclass = ["selected"];
  $scope.mclass = ["selected"];
  $scope.eclass = ["selected"];
  $scope.rclass = ["selected"];
  $scope.pclass = ["selected"];
  $scope.etclass = ["selected"];
  $scope.eeclass = ["selected"];
  $scope.eteclass = ["selected"];
  $scope.etcclass = ["selected"];
  $scope.exclass = ["selected"];
  $scope.dclass = ["selected"];
  $scope.iclass = ["selected"];
  $scope.energyLabel = "w";
  $scope.expectPeriod = "tomorrow";
  $scope.energyPeriod = "d";
  $scope.isShowEnergyData = true;
  $scope.isShowRealtimeEnergy = true;
  $scope.realtimeLabel = "w";
  $scope.planLabel = "w";
  $scope.selectPlanYear = 2019;
  $scope.planYears = [2019, 2018, 2017];
  $scope.$watch("selectPlanYear", function (newValue, oldValue) {
    if (oldValue !== newValue) {
      $scope.changePlanType($scope.planLabel);
    }
  });
  $scope.temp = function (f) {
    alert(`【${f}】尚未开放，敬请期待！`);
    throw "我是善意的错误";
  };
  $scope.updateTime = function () {
    let _this = this;
    let padStart = function (num) {
      let str = num + "";
      return str.padStart(2, "0");
    };
    setInterval(function () {
      let d = new Date();
      _this.$apply(function () {
        _this.time =
          padStart(d.getHours()) +
          ":" +
          padStart(d.getMinutes()) +
          ":" +
          padStart(d.getSeconds());
      });
    }, 1000);
  };
  $scope.updateTime();
  $scope.hideGlass = function () {
    $(".glass-wrapper .glass-left-wrapper").css("left", "-700px");
    $(".glass-wrapper .glass-right-wrapper").css("right", "-700px");
  };
  $scope.changebclass = function (i) {
    $scope.bclass = [];
    $scope.bclass[i] = "selected";
  };
  $scope.loadBuiling = function () {
    var iframe = document.getElementsByName("main")[0];
    iframe.contentWindow && iframe.contentWindow.restore();
  };
  $scope.loadData = function (type) {
    if ($scope.data.isLoad) {
      $scope.handleData(type);
    } else {
      loadJSON("3d", function (data) {
        $scope.data = data;
        data.isLoad = true;
        $scope.handleData(type);
      });
    }
  };
  $scope.handleData = function (type) {
    let data = $scope.data;
    let options;
    switch (type) {
      case "me":
        $(".message-wrapper .glass-right-wrapper").css("right", "10px");
        break;
      case "ho":
        Highcharts.chart("hc-energy", data.home.energy.options);
        Highcharts.chart("hc-park", data.home.park.options);
        Highcharts.chart("hc-alarm", data.home.alarm.options);
        $(".home-wrapper .glass-left-wrapper").css("left", "10px");
        $(".home-wrapper .glass-right-wrapper").css("right", "10px");
        break;
      case "fi":
        Highcharts.chart("hc-equipment", data.fire.equipment.options);
        Highcharts.chart("hc-track", data.fire.track.options);
        Highcharts.chart("hc-level", data.fire.level.options);
        $(".fire-wrapper .glass-left-wrapper").css("left", "10px");
        $(".fire-wrapper .glass-right-wrapper").css("right", "10px");
        break;
      case "se":
        Highcharts.chart("hc-alarm-type", data.security.alarm.options);
        Highcharts.chart("hc-level-s", data.fire.level.options);
        break;
      case "eq":
        Highcharts.chart("hc-exception", data.equipment.exception.options);
        Highcharts.chart("hc-equipment-e", data.equipment.equipment.options);
        Highcharts.chart("hc-subsystem", data.equipment.subsystem.options);
        break;
      case "op":
        Highcharts.chart("hc-people", data.operate.todayPeriod.options);
        Highcharts.chart("hc-park-o", data.operate.todayPark.options);
        options = data.operate.saveTarget.options;
        if (
          options.plotOptions &&
          options.plotOptions.column &&
          options.plotOptions.column.dataLabels
        ) {
          options.plotOptions.column.dataLabels.formatter = function () {
            let label = this.x === "电" ? "kWh" : "m<sup>3</sup>";
            return this.y + label;
          };
        }
        Highcharts.chart("hc-save-target", options);
        break;
      case "ma":
        Highcharts.chart("hc-order", data.maintain.order.options);
        Highcharts.chart("hc-fix", data.maintain.fix.options);
        Highcharts.chart("hc-patrol", data.maintain.patrolResult.options);
        break;
      case "sa":
        options = data.save.top.options;
        if (options.yAxis && options.yAxis.stackLabels) {
          options.yAxis.stackLabels.formatter = function () {
            return (
              this.total - this.points[0][0] + "kWh / " + this.total + "kWh"
            );
          };
        }
        Highcharts.chart("hc-energy-top5", options);
        $scope.changeEnergyType("w");
        break;
    }
  };
  // 首页
  $scope.home = function () {
    $scope.hideGlass();
    $scope.changebclass(0);
    setTimeout(function () {
      $scope.loadData("ho");
      $scope.loadBuiling();
      // $('.fixed').css('top', 0);
      // $('.bottom').css('bottom', "100px");
    }, $scope.glassDelay);
  };
  // 消防
  $scope.fire = function () {
    $scope.hideGlass();
    $scope.changebclass(1);
    setTimeout(function () {
      $scope.loadData("fi");
      $scope.loadBuiling();
    }, $scope.glassDelay);
  };
  // 安防
  $scope.security = function () {
    $scope.hideGlass();
    $scope.changebclass(2);
    setTimeout(function () {
      $scope.loadData("se");
      $scope.loadBuiling();
      $(".security-wrapper .glass-left-wrapper").css("left", "20px");
      $(".security-wrapper .glass-right-wrapper").css("right", "20px");
    }, $scope.glassDelay);
  };
  // 设备
  $scope.equipment = function () {
    $scope.hideGlass();
    $scope.changebclass(3);
    setTimeout(function () {
      $scope.loadData("eq");
      $scope.loadBuiling();
      $(".equipment-wrapper .glass-left-wrapper").css("left", "20px");
      $(".equipment-wrapper .glass-right-wrapper").css("right", "20px");
    }, $scope.glassDelay);
  };
  // 运营
  $scope.operate = function () {
    $scope.hideGlass();
    $scope.changebclass(4);
    setTimeout(function () {
      $scope.loadData("op");
      $scope.loadBuiling();
      $(".operate-wrapper .glass-left-wrapper").css("left", "20px");
      $(".operate-wrapper .glass-right-wrapper").css("right", "20px");
    }, $scope.glassDelay);
  };
  // 运维
  $scope.maintain = function () {
    $scope.hideGlass();
    $scope.changebclass(5);
    setTimeout(function () {
      $scope.loadData("ma");
      $scope.loadBuiling();
      $(".maintain-wrapper .glass-left-wrapper").css("left", "20px");
      $(".maintain-wrapper .glass-right-wrapper").css("right", "20px");
    }, $scope.glassDelay);
  };
  // 节能
  $scope.save = function () {
    $scope.hideGlass();
    $scope.changebclass(6);
    setTimeout(function () {
      $scope.loadData("sa");
      $scope.loadBuiling();
      $(".save-wrapper .glass-left-wrapper").css("left", "20px");
      $(".save-wrapper .glass-right-wrapper").css("right", "20px");
    }, $scope.glassDelay);
  };
  $scope.showEnergyData = function (is) {
    $scope.energyPeriod = "d";
    $scope.eeclass = [];
    if (is) {
      $scope.isShowEnergyData = true;
      $scope.eeclass[0] = "selected";
      $scope.eteclass = ["selected"];
    } else {
      $scope.isShowEnergyData = false;
      $scope.eeclass[1] = "selected";
      $scope.etcclass = ["selected"];
    }
  };
  $scope.changeEnergyData = function (period, i) {
    $scope.energyPeriod = period;
    if ($scope.isShowEnergyData) {
      $scope.eteclass = [];
      $scope.eteclass[i] = "selected";
    } else {
      $scope.etcclass = [];
      $scope.etcclass[i] = "selected";
    }
  };
  $scope.changeEnergyExpect = function (period, i) {
    $scope.expectPeriod = period;
    $scope.exclass = [];
    $scope.exclass[i] = "selected";
  };
  $scope.changeEnergyType = function (type) {
    $scope.energyLabel = type;
    $scope.etclass = [];
    let data = $scope.data;
    switch (type) {
      case "w":
        $scope.etclass[0] = "selected";
        Highcharts.chart("hc-energy-today", data.save.today.water.options);
        break;
      case "e":
        $scope.etclass[1] = "selected";
        Highcharts.chart("hc-energy-today", data.save.today.electric.options);
        break;
      case "g":
        $scope.etclass[2] = "selected";
        Highcharts.chart("hc-energy-today", data.save.today.gas.options);
        break;
    }
  };
  // 能耗
  $scope.showEnergy = function () {
    $scope.showRealtimeEnergy(1);
    $(".home-wrapper .glass-right-wrapper").css("right", "-700px");
    $(".energy-wrapper .glass-right-wrapper").css("right", "10px");
  };
  $scope.showRealtimeEnergy = function (is) {
    $scope.eclass = [];
    $scope.rclass = [];
    $scope.pclass = [];
    let data = $scope.data;
    if (is) {
      $scope.eclass[0] = "selected";
      $scope.isShowRealtimeEnergy = true;
      $scope.realtimeLabel = "w";
      $scope.rclass[0] = "selected";
      Highcharts.chart("hc-realtime", data.energy.realtime.water.options);
    } else {
      $scope.eclass[1] = "selected";
      $scope.isShowRealtimeEnergy = false;
      $scope.planLabel = "w";
      $scope.pclass[0] = "selected";
      $scope.selectPlanYear = 2019;
      Highcharts.chart(
        "hc-plan",
        data.energy.plan.water[$scope.selectPlanYear].options
      );
    }
  };
  $scope.changeRealtimeType = function (type) {
    let data = $scope.data;
    $scope.rclass = [];
    $scope.realtimeLabel = type;
    switch (type) {
      case "w":
        $scope.rclass[0] = "selected";
        Highcharts.chart("hc-realtime", data.energy.realtime.water.options);
        break;
      case "e":
        $scope.rclass[1] = "selected";
        Highcharts.chart("hc-realtime", data.energy.realtime.electric.options);
        break;
      case "g":
        $scope.rclass[2] = "selected";
        Highcharts.chart("hc-realtime", data.energy.realtime.gas.options);
        break;
    }
  };
  $scope.changePlanType = function (type) {
    let data = $scope.data;
    $scope.pclass = [];
    $scope.planLabel = type;
    switch (type) {
      case "w":
        $scope.pclass[0] = "selected";
        Highcharts.chart(
          "hc-plan",
          data.energy.plan.water[$scope.selectPlanYear].options
        );
        break;
      case "e":
        $scope.pclass[1] = "selected";
        Highcharts.chart(
          "hc-plan",
          data.energy.plan.electric[$scope.selectPlanYear].options
        );
        break;
      case "g":
        $scope.pclass[2] = "selected";
        Highcharts.chart(
          "hc-plan",
          data.energy.plan.gas[$scope.selectPlanYear].options
        );
        break;
    }
  };
  // 消息
  $scope.showMessage = function () {
    $scope.hideGlass();
    $scope.mclass = ["selected"];
    $scope.messageType = "alarm";
    setTimeout(function () {
      $scope.loadData("me");
    }, $scope.glassDelay);
    $scope.messageClass = "";
  };
  $scope.changeMessageType = function (type, i) {
    $scope.messageType = type;
    $scope.mclass = [];
    $scope.mclass[i] = "selected";
  };
  $scope.focusIncident = function (id) {
    var iframe = document.getElementsByName("main")[0];
    iframe.contentWindow && iframe.contentWindow.focusIncident(id);
  };
  // 设备信息
  $scope.showDeviceInfo = function (id) {
    if ($scope.deviceData.isLoad) {
      $scope.handleDeviceData(id);
    } else {
      loadJSON("device", function (data) {
        $scope.deviceData = data;
        $scope.deviceData.isLoad = true;
        $scope.handleDeviceData(id);
      });
    }
  };
  $scope.handleDeviceData = function (id) {
    $scope.device = $scope.deviceData[id];
    $scope.device.info = $scope.device.attribute;
    $scope.dclass = ["selected"];
    $scope.hideGlass();
    setTimeout(function () {
      $(".device-wrapper .glass-right-wrapper").css("right", "10px");
    }, $scope.glassDelay);
  };
  $scope.changeDeviceInfo = function (is) {
    $scope.dclass = [];
    $scope.dclass[is ? 0 : 1] = "selected";
    $scope.device.info = is ? $scope.device.attribute : $scope.device.param;
  };
  $scope.showIncidentInfo = function (id) {
    if ($scope.deviceData.isLoad) {
      $scope.handleIncidentData(id);
    } else {
      loadJSON("device", function (data) {
        $scope.deviceData = data;
        $scope.deviceData.isLoad = true;
        $scope.handleIncidentData(id);
      });
    }
  };
  $scope.handleIncidentData = function (id) {
    $scope.device = $scope.deviceData[id];
    $scope.device.info = $scope.device.incident;
    $scope.iclass = ["selected"];
    $scope.hideGlass();
    setTimeout(function () {
      $(".incident-wrapper .glass-right-wrapper").css("right", "10px");
    }, $scope.glassDelay);
  };
  $scope.changeIncidentInfo = function (is) {
    $scope.iclass = [];
    $scope.iclass[is ? 0 : 1] = "selected";
    $scope.device.info = is ? $scope.device.incident : $scope.device.history;
  };
});

function closeWrapper(e) {
  let div = e.parentElement;
  if (div.classList.contains("glass-left-wrapper")) {
    $(div).css("left", "-700px");
  }
  if (div.classList.contains("glass-right-wrapper")) {
    $(div).css("right", "-700px");
  }
  if (
    div.parentElement &&
    div.parentElement.classList.contains("energy-wrapper")
  ) {
    $(".home-wrapper .glass-right-wrapper").css("right", "10px");
  }
  // if (div.parentElement && div.parentElement.classList.contains('message-wrapper')) {
  // let $scope = $('div[ng-controller="controller"').scope();
  // let index = $scope.bclass.indexOf('selected');
  // switch (index) {
  //     case 0:
  //         $scope.home();
  //         break;
  //     case 1:
  //         $scope.fire();
  //         break;
  //     case 2:
  //         $scope.security();
  //         break;
  //     case 3:
  //         $scope.equipment();
  //         break;
  //     case 4:
  //         $scope.operate();
  //         break;
  //     case 5:
  //         $scope.maintain();
  //         break;
  //     case 6:
  //         $scope.save();
  //         break;
  // }
  // }
}
